<template>
  <div style="margin-top: 20px; padding: 20px 300px 10px 60px">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="昵称">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="身份">
        <el-select v-model="form.type" placeholder="请选择身份">
          <el-option label="管理员" value="1"></el-option>
          <el-option label="普通用户" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio value="0" :label="0">正常</el-radio>
          <el-radio value="1" :label="1">封禁</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          :action=upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.avatar" :src="form.avatar" class="avatar"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {saveUser} from "@/api/user";
  import {getUser} from "@/api/user";
  import {updateUser} from "@/api/user";
  import Global from "../../../../public/js/Golbal"

  export default {
    data() {
      return {
        upload: Global.baseUrl,
        form: {
          nickname: "",
          type: 0,
          status: 0,
          avatar: "",
          username: "",
          userId: 0,
        },
      };
    },
    created() {
      this.getUserInfo();
    },
    methods: {
      update() {
        this.form.userId = this.$route.query.userId;
        updateUser(this.form)
          .then((res) => {
            console.log(res);
            this.$router.push("/user/list");
          })
          .catch((reason) => {
          });
      },
      getUserInfo() {
        let id = this.$route.query.userId;
        console.log(this.$route.query.userId);
        getUser(id)
          .then((res) => {
            console.log(res);
            this.form = res.data;
          })
          .catch((reason) => {
          });
      },

      onSubmit() {
        console.log("submit!");
      },
      handleAvatarSuccess(res, file) {
        // console.log(res);
        this.form.avatar = res.data;
        // console.log(file);
        // this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === "image/jpeg";
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error("上传头像图片只能是 JPG 格式!");
        }
        if (!isLt2M) {
          this.$message.error("上传头像图片大小不能超过 2MB!");
        }
        return isJPG && isLt2M;
      },
    },
  };
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
